window.onload = function () {
    $username = $('.header .header-c .header-right .username')
    $decreaseButton = $('.product_details .main .quantity-selector .decrease');
    $increaseButton = $('.product_details .main .quantity-selector .increase');
    $quantityInput = $('.product_details .main .quantity-selector .quantity');

    axios.get("http://localhost:8080/getUserInfo")
        .then(function (response) {
            console.log(response)
            let username = response.data.data
            $username.text(username);
        })
        .catch(function (error) {
            console.log(error)
        })


    let maxQuantity

    $productDisplayArea = $('.product_details .main .detailsArea')
    $addbtn = $(".product_details .main .add")
    axios.get("http://localhost:8080/getProductDetails")
        .then(function (response) {
            console.log(response);
            // let product_details = JSON.stringify(response.data)
            // $productDisplayArea.text(product_details)
            let book_details = response.data
            maxQuantity = book_details.inventory
            let detailsHTML = `<div class="bookContent">
                <h1>${book_details.title}</h1>
                <div class="info">
                <p><strong>作者:</strong> ${book_details.author}</p>
                <p><strong>类别:</strong> ${book_details.category}</p>
                <p><strong>价格:</strong> ￥${book_details.price}</p>
                <p><strong>库存:</strong> ${book_details.inventory}</p>
                </div>
                <div class="description">
                <p><strong>描述:</strong>  ${book_details.description}</p>
                </div>
                </div>`;
            $productDisplayArea.append(detailsHTML);
        })
        .catch(function (error) {
            console.log(error)
        })

    $addbtn.on('click', function () {
        $.post("http://localhost:8080/addOrder", { "quantity": $quantityInput.text() }, function (response) {
            console.log(response);
            alert("添加成功！")
        })
    });


    $decreaseButton.on('click', () => {
        let currentValue = parseInt($quantityInput.text());
        if (currentValue > 1) {
            $quantityInput.text(currentValue - 1);
        }
    });

    $increaseButton.on('click', () => {
        let currentValue = parseInt($quantityInput.text());
        console.log(currentValue);
        if (currentValue < maxQuantity) {
            $quantityInput.text(currentValue + 1);
        }
    });


}
